﻿package com.firewolf.iteratorAndComponent.iterator.java.iterators;

import java.util.Iterator;

import com.firewolf.iteratorAndComponent.iterator.common.MyMenuItem;

/**
 * 晚餐餐菜单迭代器，实现java.util.iterator接口
 * @author Administrator
 *
 */
@SuppressWarnings("rawtypes")
public class DinerMenuIterator implements Iterator{
	private MyMenuItem[] items;
	private int index = 0;
	public DinerMenuIterator(MyMenuItem[] items) {
		this.items = items;
	}
	@Override
	public boolean hasNext() {
		if(index >= items.length || items[index] == null)
			return false;
		else
			return true;
	}

	@Override
	public Object next() {
		MyMenuItem menuItem = items[index];
		index = index+1;
		return menuItem;
	}
	@Override
	public void remove() {//实现remove方法
		if(index <= 0){
			throw new IllegalStateException("You can't remove an item until you have done at least one next()");
		}
		if(items[index-1] != null){
			for(int i = index-1;i<items.length-1;i++){
				items[i] = items[i+1];
			}
			items[items.length-1] = null;
		}
	}
}
